/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database_client;

import DataObject.MyData;
import DataObject.MyRowData;
import DataObject.TableObject;
import Shared.PackageTransfer;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

/**
 *
 * @author Backkom
 */
public class ListenFromServerRunable implements Runnable {

    ObjectInputStream ois;
    MainFrame mainDesign;
    boolean maintain = false;

    public ListenFromServerRunable(ObjectInputStream is, MainFrame mainView) {
        ois = is;
        mainDesign = mainView;
    }

    @Override
    public void run() {
        try {
            boolean getData = false;
            do {
                PackageTransfer pk = (PackageTransfer) ois.readObject();
                if (FilterHeader(pk)) {
                    String hd = pk.header;
                    if (hd.compareToIgnoreCase("ERR") != 0) {
                        if (hd.compareToIgnoreCase("Result") == 0 && !getData) {
                            //Start data add to table
                            mainDesign.listObject.clear();
                            mainDesign.tbHeader = ParseHeader2TableHeader((MyRowData) pk.datas);
                            getData = true;
                            continue;
                        }
                        if (hd.compareToIgnoreCase("Finish") == 0 && getData) {
                            //Finist get data
                            getData = false;
                            mainDesign.setEnableSend(true);
                            //mainDesign.UpdateTable();
                            continue;
                        }
                        if (hd.compareToIgnoreCase("row") == 0 && getData) {
                            //Finist get data                          
                            MyRowData row = (MyRowData) pk.datas;
                            mainDesign.listObject.add(ParseRow2TableRow(row));
                            try {
                                SwingUtilities.invokeAndWait(run);
                            } catch (InterruptedException | InvocationTargetException ex) {
                                System.out.println(ex.toString());
                            }
                        }
                    }
                } else {
                    break;
                }
            } while (true);
        } catch (IOException | ClassNotFoundException ex1) {
            JOptionPane.showMessageDialog(mainDesign, "Server interrupt connect!\n", "Report", JOptionPane.ERROR_MESSAGE);
            if (!mainDesign.sv.isClosed()) {
                try {
                    mainDesign.sv.close();
                } catch (IOException ex) {
                }
            }
        }
    }

    private boolean FilterHeader(PackageTransfer pk) {
        String header = pk.header;
        if (header.compareToIgnoreCase("ABORT") == 0) //Error transfer
        {
            String err = "Error on transfer data (interrupt): " + pk.datas.toString();
            JOptionPane.showMessageDialog(mainDesign, err, "Report", JOptionPane.ABORT);
            return false;
        }

        if (header.compareToIgnoreCase("success") == 0) //Error transfer
        {
            String err = pk.datas.toString();
            JOptionPane.showMessageDialog(mainDesign, err, "Report", JOptionPane.INFORMATION_MESSAGE);
            mainDesign.setEnableSend(true);
            return true;
        }

        if (header.compareToIgnoreCase("ERR") == 0) //Error process query
        {
            String err = pk.datas.toString();
            JOptionPane.showMessageDialog(mainDesign, err, "Report", JOptionPane.ERROR_MESSAGE);
            mainDesign.setEnableSend(true);
            return true;
        }

        if (header.compareToIgnoreCase("MAINTAIN") == 0) //Server maintain
        {
            String err = pk.datas.toString();
            JOptionPane.showMessageDialog(mainDesign, err, "Report", JOptionPane.WARNING_MESSAGE);
            mainDesign.setEnableSend(true);
            return true;
        }

        if (header.compareToIgnoreCase("nonresult") == 0) //nonresult
        {
            String err = "Not have result";
            JOptionPane.showMessageDialog(mainDesign, err, "Report", JOptionPane.INFORMATION_MESSAGE);
            mainDesign.setEnableSend(true);
            return true;
        }
        return true;
    }

    private String[] ParseHeader2TableHeader(MyRowData Header) {
        int size = Header.Datas.size();
        String[] tbHeader = new String[size];
        Vector<MyData> dt = (Vector<MyData>) Header.Datas;
        for (int j = 0; j < size; j++) {
            tbHeader[j] = dt.get(j).Column;
        }
        /*
         * for (int i = 0; i < size; i++) { MyData data = (MyData)
         * Header.Datas.get(i); if (data.isKey) { tbHeader[i] = data.Column +
         * "*"; } else { tbHeader[i] = data.Column; }
         *
         * }
         */
        return tbHeader;
    }

    private TableObject ParseRow2TableRow(MyRowData Row) {
        TableObject obj = null;
        int size = mainDesign.tbHeader.length;
        Object[] temp = new Object[size];

        Vector<MyData> dt = (Vector<MyData>) Row.Datas;
        for (int j = 0; j < size; j++) {
            temp[j] = dt.get(j).Value;
        }
        /*
         * for (int i = 0; i < mainDesign.tbHeader.length; i++) { String
         * columnName = mainDesign.tbHeader[i]; for (int j = 0; j <
         * mainDesign.tbHeader.length; j++) { MyData data = ((MyData)
         * Row.Datas.elementAt(j)); if (data.Column.compareTo(columnName) == 0)
         * { temp[i] = data.Value.toString(); break; } }
         *
         * }
         */
        obj = new TableObject(temp);
        return obj;
    }
    Runnable run = new Runnable() {

        @Override
        public void run() {
            mainDesign.UpdateTable();
        }
    };
}
